<?php

namespace app\user\controller;

use app\common\respond\HttpApi;
use app\user\logic\MachineLogic;

//上位机
class MachineController
{
    /**
     * @Description 绑定
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/03/27 12:04:11
     * @return \think\response\Json
     */
    public function bind(HttpApi $respond)
    {
        $param = [
            'machine_code' => request()->post('machine_code'),
        ];
        $rule = [
            'machine_code' => 'require|max:64',
        ];
        validate($rule)->check($param);

        $factoryId = request()->user_info['factory_id'];
        $logic = new MachineLogic();
        $logic->bind($factoryId, $param['machine_code']);

        return $respond->success([]);
    }

    /**
     * @Description 列表
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/03/27 12:04:23
     * @return \think\response\Json
     */
    public function list(HttpApi $respond)
    {
        $param = [
            'machine_code' => request()->get('machine_code'),
            'page' => (int)(request()->get('page')?: 1),
            'page_size' => (int)(request()->get('page_size')?: 10),
        ];
        $rule = [
            'machine_code' => 'max:64',
            'page' => '>:0',
            'page_size' => '>:0',
        ];
        validate($rule)->check($param);

        $factoryId = request()->user_info['factory_id'];
        $logic = new MachineLogic();
        $data = $logic->getList($factoryId, $param);

        return $respond->success($data);
    }

    /**
     * @Description 详情
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/03/27 12:04:32
     * @return \think\response\Json
     */
    public function detail(HttpApi $respond)
    {
        $param = [
            'machine_id' => (int)request()->get('machine_id'),
        ];
        $rule = [
            'machine_id' => '>:0',
        ];
        validate($rule)->check($param);

        $factoryId = request()->user_info['factory_id'];
        $logic = new MachineLogic();
        $data = $logic->getDetail($factoryId, $param['machine_id']);

        return $respond->success($data);
    }

    /**
     * @Description 解绑
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/03/27 12:04:40
     * @return \think\response\Json
     */
    public function unbind(HttpApi $respond)
    {
        $param = [
            'machine_id' => (int)request()->post('machine_id'),
        ];
        $rule = [
            'machine_id' => '>:0',
        ];
        validate($rule)->check($param);

        $factoryId = request()->user_info['factory_id'];
        $logic = new MachineLogic();
        $logic->unbind($factoryId, $param['machine_id']);

        return $respond->success([]);
    }
}